//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CBaseVertex10.cpp>
///	@path	~/src/graphics/dx10/
///	@date	2009/01/13
///	@desc	.

#include "config/config.h"

#include "graphics/dx10/CBaseVertex10.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CBaseVertex10 );

	// CBaseVertex10
	CBaseVertex10::CBaseVertex10( ID3D10Buffer * buffer , size_t count , size_t stride )
		: m_pBuffer( buffer )
		, m_vertexCount( count )
		, m_stride( stride )
	{
		m_pBuffer->GetDesc( &m_desc );
	}
	// ~CBaseVertex10
	CBaseVertex10::~CBaseVertex10( void )
	{
		if( m_pBuffer )
		{
			UINT rest = m_pBuffer->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CBaseVertex10::DTOR: Invalid ID3D10Buffer reference count.") );
				FATAL_ABORT( _S("Invalid ID3D10Buffer reference count.") );
			}
		}
	}
	// OnLostDevice
	bool CBaseVertex10::OnLostDevice( void )
	{
		return true;
	}
	// OnResetDevice
	bool CBaseVertex10::OnResetDevice( RefWeak<IDevice> device )
	{
		return true;
	}
	// GetCount
	size_t CBaseVertex10::GetCount( void )
	{
		return m_vertexCount;
	}
	// GetSize
	size_t CBaseVertex10::GetSize( void )
	{
		return m_desc.ByteWidth;
	}
	// IsWritable
	bool CBaseVertex10::IsWritable( void )
	{
		return (m_desc.CPUAccessFlags & D3D10_CPU_ACCESS_WRITE) ? true : false;
	}
		// IsReadable
	bool CBaseVertex10::IsReadable( void )
	{
		return (m_desc.CPUAccessFlags & D3D10_CPU_ACCESS_READ) ? true : false;
	}
	// Map
	bool CBaseVertex10::Map( bool discard , ResourceMap& data )
	{
		TRACE_ERROR( _S("CBaseVertex10::Map: Not implemented.") );
		FATAL_ABORT( _S("Not implemented call.") );
		return false;
	}
	// ReadBack
	bool CBaseVertex10::ReadBack( ResourceMap& data , bool readonly )
	{
		TRACE_ERROR( _S("CBaseVertex10::ReadBack: Not implemented.") );
		FATAL_ABORT( _S("Not implemented call.") );
		return false;
	}
	// Unmap
	bool CBaseVertex10::Unmap( void )
	{
		TRACE_ERROR( _S("CBaseVertex10::Unmap: Not implemented.") );
		FATAL_ABORT( _S("Not implemented call.") );
		return false;
	}
	// GetVertexInterface
	IUnknown * CBaseVertex10::GetVertexInterface( void )
	{
		return m_pBuffer;
	}
	// GetIndexInterface
	IUnknown * CBaseVertex10::GetIndexInterface( void )
	{
		return NULL;
	}
	// GetStreamCount
	uint CBaseVertex10::GetStreamCount( void )
	{
		return 1;
	}
	// ApplyStream
	void CBaseVertex10::ApplyStream( RenderContext& rc , uint offset_vertex )
	{
		UINT stride = m_stride;
		UINT offset = stride * offset_vertex;
		rc.GetDX10()->IASetVertexBuffers( 0 , 1 , &m_pBuffer , &stride , &offset );
	}

} // namespace xeres
